package done.myra.excercise.ch9;

/*
 * 利用集合的知识list set map
 * 根据表格信息打印输出
 * 数据的结构
 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.*;
import java.util.Set;

public class TestCollect {

	public static void main(String[] args) {

		test03();
	}
	
	
	/**
	 * 输出表格信息
	 * 有用泛型
	 * 方法拆分
	 * 1，数据准备
	 * 2.打印输出
	 */
	private static void test03() {
		//定义链表
		List<Map<String,Object>> stuList = new ArrayList<>();
		//用map集合存储学生信息
		Map<String, Object> mStu1 = new LinkedHashMap<>();
		mStu1.put("stuName", "小明");
		mStu1.put("stuAge", 18);
		mStu1.put("stuSex", "男");
		mStu1.put("stuNum", "15001");
		mStu1.put("stuAddress", "天津东丽");
		
		Map<String, Object> mStu2 = new LinkedHashMap<>();
		mStu2.put("stuName", "夏利");
		mStu2.put("stuAge", 20);
		mStu2.put("stuSex", "女");
		mStu2.put("stuNum", "15003");
		mStu2.put("stuAddress", "天津北辰");
		//将学生信息放入list
		stuList.add(mStu1);
		stuList.add(mStu2);
		
		//
		printList(stuList);
				
	}
	
	private static void printList(List<Map<String,Object>> stuList) {

		if(stuList!=null && stuList.size()>0) {
		//迭代器
		Iterator<Map<String, Object>> it = stuList.iterator();
		boolean isTitle = true;
		
		//输出表格格式
		while(it.hasNext()) {
			//获取元素
			Map<String, Object> stuMap =it.next();
			//转化为Set集合
			Set<Entry<String, Object>> stus = stuMap.entrySet();
			
			//遍历输出set集合
			//输出表头
			
			if(isTitle) {
				for (Entry<String, Object> entrys : stus) {
					System.out.print("\t"+entrys.getKey());
					isTitle = false;
				}
				System.out.println();
			}
//			
			//输出内容
			for (Entry<String, Object> entrys : stus) {
				System.out.print("\t"+entrys.getValue());
			}
			//换行
			System.out.println();
			
		}
		
		}
	
	}
	
	
	/**
	 * 输出表格信息
	 * 有用泛型
	 */
	private static void test02() {
		//定义链表
		List<Map<String,Object>> stuList = new ArrayList<>();
		//用map集合存储学生信息
		Map<String, Object> mStu1 = new LinkedHashMap<>();
		mStu1.put("stuName", "小明");
		mStu1.put("stuAge", 18);
		mStu1.put("stuSex", "男");
		mStu1.put("stuNum", "15001");
		mStu1.put("stuAddress", "天津东丽");
		
		Map<String, Object> mStu2 = new LinkedHashMap<>();
		mStu2.put("stuName", "夏利");
		mStu2.put("stuAge", 20);
		mStu2.put("stuSex", "女");
		mStu2.put("stuNum", "15003");
		mStu2.put("stuAddress", "天津北辰");
		//将学生信息放入list
		stuList.add(mStu1);
		stuList.add(mStu2);
		
		
		//迭代器
		Iterator<Map<String, Object>> it = stuList.iterator();
		
		//输出表格格式
		if(it.hasNext()) {
			//获取元素
			Map<String, Object> stuMap =it.next();
			//转化为Set集合
			Set<Entry<String, Object>> stus = stuMap.entrySet();
			//遍历输出set集合
			for (Entry<String, Object> entrys : stus) {
				System.out.print(" "+entrys.getKey()+" ");
			}
			
		}
		//换行
		System.out.println();
		//输出学生信息
		//学生链表的迭代器
		it = stuList.iterator();
		//如果有下一个元素
		while(it.hasNext()) {
			//获取元素
			Map<String,Object> stuMap =it.next();
			//转化为Set集合
			Set<Entry<String, Object>> stus = stuMap.entrySet();
			//遍历输出set集合
			for (Entry<String, Object> entrys : stus) {
				System.out.print("     ");
				System.out.print(entrys.getValue());
				System.out.print("   ");
			}
			//换行
			System.out.println();
			
		}
		
	}

	
	
	/**
	 * 输出表格信息
	 * 没有用泛型
	 */
	private static void test01() {
		//定义链表
		List stuList = new ArrayList();
		//用map集合存储学生信息
		Map<String, Object> mStu1 = new LinkedHashMap<>();
		mStu1.put("stuName", "小明");
		mStu1.put("stuAge", 18);
		mStu1.put("stuSex", "男");
		mStu1.put("stuNum", "15001");
		mStu1.put("stuAddress", "天津东丽");
		
		Map<String, Object> mStu2 = new LinkedHashMap<>();
		mStu2.put("stuName", "夏利");
		mStu2.put("stuAge", 20);
		mStu2.put("stuSex", "女");
		mStu2.put("stuNum", "15003");
		mStu2.put("stuAddress", "天津北辰");
		//将学生信息放入list
		stuList.add(mStu1);
		stuList.add(mStu2);
		
		
		//遍历
		Iterator it = stuList.iterator();
		//信息对应输出
//		while(it.hasNext()) {
//			Map stuMap = (Map)it.next();
//			Set stus = stuMap.entrySet();
//			for (Object obj : stus) {
//				Entry  ens = (Entry) obj;
//				System.out.print(ens.getKey()+"-"+ens.getValue()+"    ");
//			}
//			
//			
//		}
		
		//输出表格格式
		if(it.hasNext()) {
			Map stuMap = (Map)it.next();
			Set stus = stuMap.entrySet();
			for (Object obj : stus) {
				Entry  ens = (Entry) obj;
				System.out.print(" "+ens.getKey()+" ");
			}	
			
		}
		System.out.println();
		//输出学生信息
		it = stuList.iterator();
		while(it.hasNext()) {
			Map stuMap = (Map)it.next();
			Set stus = stuMap.entrySet();
			for (Object obj : stus) {
				Entry  ens = (Entry) obj;
				System.out.print("     ");
				System.out.print(ens.getValue());
				System.out.print("   ");
			}
			System.out.println();
			
		}
		
	}

}
